#include<string>
#include<cstddef>
#include<iostream>

#define __DEBUG__

using std::string;

class String;

class StringCollection {
	String const * left;
	StringCollection * right;
	size_t len;
	
	StringCollection() {}
	
	public:
		
	StringCollection(String const &); //: left(& s), right(0), len(s . length()) {}
	StringCollection(StringCollection const & sc) : left(sc . left), right(sc . right ? new StringCollection(* sc . right) : 0), len(sc . len) {}
	~StringCollection() {
		delete right;
	}
	
	friend class String;
		
	friend StringCollection operator+(StringCollection const &, StringCollection const &);
};

class String {
	
	string s;
	
	public:
		
	String() : s() {}
		
	String(string const & st) : s(st) {}
		
	String(char const * st) : s(st) {}
		
	String(String const & st) : s(st . s) {}
		
	String(StringCollection const & sc) : s() {
		#ifdef __DEBUG__
			std::cout << "String from StringCollection" << std::endl;
		#endif
		if (sc . left == 0)
			return;
		s . resize(sc . len);
		StringCollection const * scp = &sc;
		size_t pos = 0, delta = 0;
		while (scp != 0) {
			s. replace(pos, string::npos, scp -> left -> s);
			pos += scp -> left -> length();
			scp = scp -> right;
		}
	}
		
	size_t length() const {
		return s . length();
	}
	
	explicit operator string() {
		#ifdef __DEBUG__
			std::cout << "typecast String -> string" << std::endl;
		#endif
		return s;
	}
		
	/*String & operator=(StringCollection const & sc) {
		#ifdef __DEBUG__
			std::cout << "String::operator=" << std::endl;
		#endif
		if (sc . left == 0)
			return operator=("");
		s. resize(sc . len);
		StringCollection const * scp = &sc;
		size_t pos = 0, delta = 0;
		while (scp != 0) {
			s. replace(pos, string::npos, scp -> left -> s);
			pos += scp -> left -> length();
			scp = scp -> right;
		}
		return *this;
	}*/
	
	operator StringCollection () {
		#ifdef __DEBUG__
			std::cout << "typecast String -> StringCollection" << std::endl;
		#endif
		return StringCollection(*this);
	}
	
};

StringCollection::StringCollection(String const & s) : left(& s), right(0), len(s . length()) {}
	
StringCollection operator+(StringCollection const & sc1, StringCollection const & sc2) {
	#ifdef __DEBUG__
		std::cout << "operator+" << std::endl;
	#endif
	StringCollection res(sc1);
	StringCollection * scp = & res;
	while (scp -> right != 0)
		scp = scp -> right;
	scp -> right = new StringCollection(sc2);
	res . len = sc1 . len + sc2 . len;
	return res;
}

